<?php


namespace app\controller\admin;


use app\BaseController;
use think\facade\Db;
use app\models\goods\CategoryModel;

class CategoryController extends BaseController
{
    public function getList()
    {
        try {
            $where = [];
            $title = $this->request->param('title','','trim');
            if ($title) {
                $where[] = ['title', 'like', '%' . $title . '%'];
            }
            $list = Db::name('category')
                ->where($where)
                ->order('id', 'asc')
                ->paginate(['list_rows' => $this->limit, 'page' => $this->page])
                ->toArray();
            foreach($list['data'] as &$item){
                $item['pid'] = $item['pid'] == 0 ? null : $item['pid'];
                $item['ptitle'] = CategoryModel::where('id',$item['pid'])->value('title');
            }
            return $this->success($list);
        } catch (\Exception $exception) {
            return $this->error($exception->getMessage());
        }
    }

    public function save()
    {
        try {
            $title = $this->request->param('title', '', 'trim');
            $img = $this->request->param('img', '', 'trim');
            $id = $this->request->param('id');
            $pid = $this->request->param('pid', 0);
            $sort = $this->request->param('sort', 0);
            if ($id) {
                $model = CategoryModel::find($id);
                if (!$model) {
                    throw new \Exception('分类不存在');
                }
                if($model->pid == $pid){
                    throw new \Exception('分类的上级不合规');
                }
                $model->title = $title;
                $model->pid = $pid;
                $model->img = $img;
                $model->sort = $sort;
                $model->update_time = 0;
                $model->save();
            } else {
                $model = new CategoryModel();
                $model->title = $title;
                $model->pid = $pid;
                $model->img = $img;
                $model->sort = $sort;
                $model->status = 0;
                $model->create_time = time();
                $model->update_time = time();
                $model->save();
            }
            return $this->success();
        } catch (\Exception $exception) {
            return $this->error($exception->getMessage());
        }
    }

    public function del()
    {
        try {
            $id = $this->request->param('id');
            $info = Db::name('category')->where('id', $id)->find();
            if(!$info){
                throw new \Exception('非法请求');
            }
            $article = Db::name('article')->where('status', 0)->where('cate_id',$id)->find();
            if($article){
                throw new \Exception('有项目使用该分类，不能删除');
            }
            Db::name('category')->where('id', $id)->delete();
            return $this->success();
        } catch (\Exception $exception) {
            return $this->error($exception->getMessage());
        }
    }


    public function getCategoryArr()
    {
        try {
            $list = CategoryModel::where('status', 0)
                //->where('pid', 0)
                ->field('id,title,pid')
                ->order('pid asc,sort asc')
                ->select()
                ->toArray();

            $list = list_to_tree($list, 'id', 'pid', 'children');
            return $this->success($list);
        } catch (\Exception $exception) {
            return $this->error($exception->getMessage());
        }
    }

    public function showIndex()
    {
        try {
            $id = $this->request->param('id');
            $status = $this->request->param('status');
            if ($status == 1) {
                $status = 0;
            } else {
                $status = 1;
            }
            Db::name('category')->where('id', $id)->update(['show_index' => $status]);

            return $this->success();
        } catch (\Exception $exception) {
            return $this->error($exception->getMessage());
        }
    }
}